import { HttpException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class ChatRoomService {
  constructor(private readonly prismaService: PrismaService) {}
  async createChatOne(friendId: number, userId: number) {
    if (friendId == userId) throw new HttpException('不能添加自己为好友', 400);

    // 判断两个用户是否已经在存在的同一个聊天室中
    const chatRoom = await this.prismaService.userChatRoom.findFirst({
      where: {
        OR: [
          {
            userId: userId,
          },
          {
            userId: friendId,
          },
        ],
      },
    });

    if (chatRoom) return;

    const user = await this.prismaService.user.findUnique({
      where: {
        id: friendId,
      },
    });

    const my = await this.prismaService.user.findUnique({
      where: {
        id: userId,
      },
    });

    const res = await this.prismaService.chatRoom.create({
      data: {
        name: my.username + '-' + userId + '-' + user.username + '-' + friendId,
      },
      select: {
        id: true,
      },
    });

    await this.prismaService.userChatRoom.create({
      data: {
        userId: userId,
        chatRoomId: +res.id,
      },
    });

    await this.prismaService.userChatRoom.create({
      data: {
        userId: friendId,
        chatRoomId: +res.id,
      },
    });

    return '创建成功';
  }

  async createChatGroup(name: string, userId: number) {
    const res = await this.prismaService.chatRoom.create({
      data: {
        name: name,
        type: 1,
      },
      select: {
        id: true,
      },
    });

    await this.prismaService.userChatRoom.create({
      data: {
        userId: userId,
        chatRoomId: +res.id,
      },
    });

    return '创建成功';
  }

  async getChatList(userId: number) {
    const res = await this.prismaService.userChatRoom.findMany({
      where: {
        userId: userId,
      },
      include: {
        chatRoom: true,
      },
    });

    return res.map((item) => {
      if (item.chatRoom.type == 0) {
        const arr = item.chatRoom.name.split('-');
        const name = arr[1] == userId + '' ? arr[2] : arr[0];
        item.chatRoom.name = name;
      }

      return item;
    });
  }

  async getGroupUser(chatRoomId: number) {
    const res = await this.prismaService.userChatRoom.findMany({
      where: {
        chatRoomId: chatRoomId,
      },

      include: {
        user: {
          select: {
            username: true,
            id: true,
          },
        },
      },
    });

    return res;
  }

  async getGroupInfo(chatRoomId: number) {
    const res = await this.prismaService.chatRoom.findUnique({
      where: {
        id: chatRoomId,
      },
    });

    return res;
  }

  async joinGroup(chatRoomId: number, userId: number) {
    const res = await this.prismaService.chatRoom.findUnique({
      where: {
        id: chatRoomId,
      },
    });

    const chatRoom = await this.prismaService.userChatRoom.findUnique({
      where: {
        userId_chatRoomId: {
          userId: userId,
          chatRoomId: chatRoomId,
        },
      },
    });

    if (chatRoom) throw new HttpException('已加入该群聊', 400);

    if (!res) throw new HttpException('群聊不存在', 400);

    if (res.type == 0) throw new HttpException('请使用加群方式加入', 400);

    await this.prismaService.userChatRoom.create({
      data: {
        userId: userId,
        chatRoomId: chatRoomId,
      },
    });

    return '加入成功';
  }

  async quitGroup(chatRoomId: number, userId: number) {
    const res = await this.prismaService.chatRoom.findUnique({
      where: {
        id: chatRoomId,
      },
    });

    if (!res) throw new HttpException('群聊不存在', 400);

    if (res.type == 0) throw new HttpException('一对一聊天不能退出', 400);

    await this.prismaService.userChatRoom.delete({
      where: {
        userId_chatRoomId: {
          userId: userId,
          chatRoomId: chatRoomId,
        },
      },
    });

    return '退出成功';
  }

  async searchGroup(keyword: string) {
    const res = await this.prismaService.chatRoom.findMany({
      where: {
        name: {
          contains: keyword,
        },
        type: 1,
      },
    });

    return res;
  }

  // getGroupList
  async getGroupList(userId: number) {
    const res = await this.prismaService.userChatRoom.findMany({
      where: {
        userId: userId,
        chatRoom: {
          type: 1,
        },
      },
      include: {
        chatRoom: true,
      },
    });

    return res;
  }
}
